package com.yanqu.road.server.channelserver.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameRankMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.enums.eEnterNameRewardTypes;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRankModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRankModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = ChannelProtocol.N_ENTER_NAME_GET_RANK_FOR_REWARD, desc = "奖励排名")
public class ChannelEnterNameGetRewardRankCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        EnterNameProto.ChannelEnterNameGetRankForRewardReqMsg reqMsg = EnterNameProto.ChannelEnterNameGetRankForRewardReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = reqMsg.getType();
        int myRank = -1;
        eEnterNameRewardTypes rewardTypes = eEnterNameRewardTypes.find(type);
        if(rewardTypes != null){
            EnterNameModel model = ChannelEnterNameMgr.getEnterNameModelMap().get(activityId);
            if(model != null) {
                //拿分组信息
                UserEnterName userEnterName = model.getUserEnterName(reqMsg.getUserKey().getUserId());
                if (userEnterName != null) {
                    ActivityGroupMatch groupMatch = model.getActivityGroupMatch(userEnterName.getGroupId());
                    if (groupMatch != null) {
                        if(rewardTypes.isGroupReward()){
                            EnterNameGroupRankModel groupRankModel = ChannelEnterNameRankMgr.getEnterNameGroupRankModel(activityId, type, groupMatch.getRankMatchGroupId());
                            if(groupRankModel != null){
                                myRank = groupRankModel.getMyRank(groupMatch.getGroupId());
                            }
                        }else {
                            EnterNameRankModel enterNameRankModel = ChannelEnterNameRankMgr.getEnterNameRankModel(activityId, type, groupMatch.getRankMatchGroupId());
                            if(enterNameRankModel != null){
                                myRank = enterNameRankModel.getMyRank(userEnterName.getUserId(), userEnterName.getServerId());
                            }
                        }
                    }
                }
            }
        }
        CrossActivityProto.CrossActivityGetRewardRespMsg.Builder gameMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
        gameMsg.setActivityId(activityId);
        gameMsg.setConditionId(reqMsg.getConditionId());
        gameMsg.setRank(myRank);
        ChannelMessageHelper.sendGamePacket(Protocol.S_CROSS_ACTIVITY_GET_RANK_REWARD_NOTIFY, gameMsg, reqMsg.getUserKey().getChannelId(), reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());
    }

    @Override
    public void sendData() {

    }
}
